package com.icongyou.enterprise.talent_analysis.mapper;

import com.icongyou.enterprise.talent_analysis.entity.AIInsightCache;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;

/**
 * AI洞察缓存Mapper测试
 */
@SpringBootTest
@Transactional
public class AIInsightCacheMapperTest {

    @Autowired
    private AIInsightCacheMapper mapper;

    // 使用数据库中存在的学生ID
    private static final String STUDENT_1 = "660e8400-e29b-41d4-a716-446655440101"; // 张三
    private static final String STUDENT_2 = "660e8400-e29b-41d4-a716-446655440102"; // 李四
    private static final String STUDENT_3 = "660e8400-e29b-41d4-a716-446655440103"; // 王五
    private static final String STUDENT_4 = "660e8400-e29b-41d4-a716-446655440104"; // 赵六
    private static final String STUDENT_5 = "660e8400-e29b-41d4-a716-446655440105"; // 孙七
    private static final String STUDENT_6 = "660e8400-e29b-41d4-a716-446655440106"; // 周八

    @BeforeEach
    public void setUp() {
        // 清理测试学生的缓存数据,确保测试独立性
        mapper.deleteByStudentId(STUDENT_1);
        mapper.deleteByStudentId(STUDENT_2);
        mapper.deleteByStudentId(STUDENT_3);
        mapper.deleteByStudentId(STUDENT_4);
        mapper.deleteByStudentId(STUDENT_5);
        mapper.deleteByStudentId(STUDENT_6);
    }

    @Test
    public void testInsertAndSelect() {
        String studentId = STUDENT_1;
        String templateId = "template001";
        
        AIInsightCache cache = new AIInsightCache();
        cache.setId(UUID.randomUUID().toString());
        cache.setStudentId(studentId);
        cache.setTemplateId(templateId);
        cache.setInsightJson("{\"studentName\":\"张三\"}");
        cache.setAiModelVersion("GPT-4");
        cache.setGeneratedAt(LocalDateTime.now());
        cache.setUpdatedAt(LocalDateTime.now());
        cache.setExpireAt(LocalDateTime.now().plusDays(7));
        cache.setVersion(1);

        int result = mapper.insert(cache);
        assertEquals(1, result);

        AIInsightCache retrieved = mapper.selectByStudentAndTemplate(studentId, templateId);
        assertNotNull(retrieved);
        assertEquals(studentId, retrieved.getStudentId());
        assertEquals(templateId, retrieved.getTemplateId());
        assertEquals("GPT-4", retrieved.getAiModelVersion());
    }

    @Test
    public void testSelectByStudentAndTemplate_withNullTemplate() {
        String studentId = STUDENT_2;
        
        AIInsightCache cache = new AIInsightCache();
        cache.setId(UUID.randomUUID().toString());
        cache.setStudentId(studentId);
        cache.setTemplateId(null); // 默认模板
        cache.setInsightJson("{\"studentName\":\"李四\"}");
        cache.setAiModelVersion("GPT-4");
        cache.setGeneratedAt(LocalDateTime.now());
        cache.setUpdatedAt(LocalDateTime.now());
        cache.setExpireAt(LocalDateTime.now().plusDays(7));
        cache.setVersion(1);

        mapper.insert(cache);

        AIInsightCache retrieved = mapper.selectByStudentAndTemplate(studentId, null);
        assertNotNull(retrieved);
        assertEquals(studentId, retrieved.getStudentId());
        assertNull(retrieved.getTemplateId());
    }

    @Test
    public void testSelectByStudentAndTemplate_expired() {
        String studentId = STUDENT_3;
        String templateId = "template002";
        
        AIInsightCache cache = new AIInsightCache();
        cache.setId(UUID.randomUUID().toString());
        cache.setStudentId(studentId);
        cache.setTemplateId(templateId);
        cache.setInsightJson("{\"studentName\":\"王五\"}");
        cache.setAiModelVersion("GPT-4");
        cache.setGeneratedAt(LocalDateTime.now().minusDays(10));
        cache.setUpdatedAt(LocalDateTime.now().minusDays(10));
        cache.setExpireAt(LocalDateTime.now().minusDays(1)); // 已过期
        cache.setVersion(1);

        mapper.insert(cache);

        // 过期的缓存不应该被查询到
        AIInsightCache retrieved = mapper.selectByStudentAndTemplate(studentId, templateId);
        assertNull(retrieved);
    }

    @Test
    public void testUpdateByIdWithVersion() {
        String studentId = STUDENT_4;
        String templateId = "template003";
        
        AIInsightCache cache = new AIInsightCache();
        String id = UUID.randomUUID().toString();
        cache.setId(id);
        cache.setStudentId(studentId);
        cache.setTemplateId(templateId);
        cache.setInsightJson("{\"version\":\"1\"}");
        cache.setAiModelVersion("GPT-4");
        cache.setGeneratedAt(LocalDateTime.now());
        cache.setUpdatedAt(LocalDateTime.now());
        cache.setExpireAt(LocalDateTime.now().plusDays(7));
        cache.setVersion(1);

        mapper.insert(cache);

        // 更新
        cache.setInsightJson("{\"version\":\"2\"}");
        cache.setUpdatedAt(LocalDateTime.now());
        cache.setExpireAt(LocalDateTime.now().plusDays(14));
        
        int result = mapper.updateByIdWithVersion(cache);
        assertEquals(1, result);

        AIInsightCache updated = mapper.selectByStudentAndTemplate(studentId, templateId);
        assertNotNull(updated);
        assertTrue(updated.getInsightJson().contains("\"2\""));
        assertEquals(2, updated.getVersion()); // 版本号应该增加
    }

    @Test
    public void testUpdateByIdWithVersion_versionConflict() {
        String studentId = STUDENT_5;
        String templateId = "template004";
        
        AIInsightCache cache = new AIInsightCache();
        String id = UUID.randomUUID().toString();
        cache.setId(id);
        cache.setStudentId(studentId);
        cache.setTemplateId(templateId);
        cache.setInsightJson("{\"version\":\"1\"}");
        cache.setAiModelVersion("GPT-4");
        cache.setGeneratedAt(LocalDateTime.now());
        cache.setUpdatedAt(LocalDateTime.now());
        cache.setExpireAt(LocalDateTime.now().plusDays(7));
        cache.setVersion(1);

        mapper.insert(cache);

        // 使用错误的版本号更新
        cache.setVersion(999); // 错误的版本号
        cache.setInsightJson("{\"version\":\"999\"}");
        
        int result = mapper.updateByIdWithVersion(cache);
        assertEquals(0, result); // 应该更新失败
    }

    @Test
    public void testDeleteByStudentId() {
        String studentId = STUDENT_6;
        
        // 插入两条记录
        for (int i = 0; i < 2; i++) {
            AIInsightCache cache = new AIInsightCache();
            cache.setId(UUID.randomUUID().toString());
            cache.setStudentId(studentId);
            cache.setTemplateId("template" + i);
            cache.setInsightJson("{}");
            cache.setAiModelVersion("GPT-4");
            cache.setGeneratedAt(LocalDateTime.now());
            cache.setUpdatedAt(LocalDateTime.now());
            cache.setExpireAt(LocalDateTime.now().plusDays(7));
            cache.setVersion(1);
            mapper.insert(cache);
        }

        int result = mapper.deleteByStudentId(studentId);
        assertEquals(2, result);

        // 验证已删除
        AIInsightCache retrieved = mapper.selectByStudentAndTemplate(studentId, "template0");
        assertNull(retrieved);
    }

    @Test
    public void testDeleteByTemplateId() {
        String templateId = "template-to-delete";
        
        // 插入多条使用同一模板的记录
        for (int i = 0; i < 3; i++) {
            AIInsightCache cache = new AIInsightCache();
            cache.setId(UUID.randomUUID().toString());
            cache.setStudentId("student" + i);
            cache.setTemplateId(templateId);
            cache.setInsightJson("{}");
            cache.setAiModelVersion("GPT-4");
            cache.setGeneratedAt(LocalDateTime.now());
            cache.setUpdatedAt(LocalDateTime.now());
            cache.setExpireAt(LocalDateTime.now().plusDays(7));
            cache.setVersion(1);
            mapper.insert(cache);
        }

        int result = mapper.deleteByTemplateId(templateId);
        assertEquals(3, result);
    }

    @Test
    public void testDeleteExpired() {
        // 插入过期和未过期的记录
        String expiredId = UUID.randomUUID().toString();
        AIInsightCache expired = new AIInsightCache();
        expired.setId(expiredId);
        expired.setStudentId("student-expired");
        expired.setTemplateId("template-expired");
        expired.setInsightJson("{}");
        expired.setAiModelVersion("GPT-4");
        expired.setGeneratedAt(LocalDateTime.now().minusDays(10));
        expired.setUpdatedAt(LocalDateTime.now().minusDays(10));
        expired.setExpireAt(LocalDateTime.now().minusDays(1)); // 已过期
        expired.setVersion(1);
        mapper.insert(expired);

        String validId = UUID.randomUUID().toString();
        AIInsightCache valid = new AIInsightCache();
        valid.setId(validId);
        valid.setStudentId("student-valid");
        valid.setTemplateId("template-valid");
        valid.setInsightJson("{}");
        valid.setAiModelVersion("GPT-4");
        valid.setGeneratedAt(LocalDateTime.now());
        valid.setUpdatedAt(LocalDateTime.now());
        valid.setExpireAt(LocalDateTime.now().plusDays(7)); // 未过期
        valid.setVersion(1);
        mapper.insert(valid);

        int result = mapper.deleteExpired();
        assertTrue(result >= 1); // 至少删除一条过期记录

        // 验证未过期的记录仍然存在
        AIInsightCache retrieved = mapper.selectByStudentAndTemplate("student-valid", "template-valid");
        assertNotNull(retrieved);
    }

    @Test
    public void testSelectByStudentAndTemplate_notFound() {
        AIInsightCache retrieved = mapper.selectByStudentAndTemplate(
            "nonexistent-student", 
            "nonexistent-template"
        );
        assertNull(retrieved);
    }
}
